package com.xcity.game.achievement.helper;

import com.xcity.game.achievement.Achievement;
import com.xcity.game.achievement.Achievement.State;
import com.xcity.game.achievement.AchievementHelper;
import com.xcity.game.achievement.AchievementTemplate;

/**
 * 成就类型：购买特定充值卡<br/>
 * 1个参数：①充值id
 * @author yang.li
 *
 */
public class RechargeSomeoneHelper implements AchievementHelper {

	@Override
	public boolean update(Achievement refer, int... vals) {
		String key = String.valueOf(obtainRechargeId(vals));
		int val = refer.getValue().getIntValue(key) + 1;
		refer.getValue().put(key, val);
		return isFinished(refer);
	}

	@Override
	public boolean isFinished(Achievement refer) {
		if (refer.getState() == State.INITED) {
			String key = String.valueOf(obtainRechargeId(refer.getTemplate()));
			if (refer.getValue().getIntValue(key) > 0) {
				refer.setState(State.FINISHED);
				return true;
			}
		}
		return false;
	}
	
	static int obtainRechargeId(int... vals) {
		return vals != null && vals.length > 0 ? vals[0] : 0;
	}
	
	static int obtainRechargeId(AchievementTemplate template) {
		return (int) template.getCondition(0);
	}
	
}
